package com.zmn.plat.services.impl.push;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.push.inner.InnerDTO;
import com.zmn.plat.model.entity.inner.InnerQuery;
import com.zmn.plat.model.entity.push.PushInner;
import com.zmn.plat.persistence.interfaces.push.PushInfoDAO;
import com.zmn.plat.services.interfaces.push.PushInfoService;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2019/1/3 10:50
 */
@Service
public class PushInfoServiceImpl implements PushInfoService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    PushInfoDAO pushInfoDAO;

    @Override
    public Integer countByQuery(InnerQuery query) {
        return pushInfoDAO.countByQuery(query);
    }

    @Override
    public InnerDTO findOneInnerMsg(Integer staffId) {
        if (staffId == null) {
            return null;
        }

        InnerDTO dto = pushInfoDAO.findOneInnerMsg(new InnerQuery(staffId, GlobalConsts.NO, null));

        if (dto == null) {
            return null;
        }
        setLink(dto);

        return dto;
    }

    @Override
    public List<InnerDTO> listPageByQuery(InnerQuery query) {
        List<InnerDTO> list = pushInfoDAO.listPageByQuery(query);

        for (InnerDTO dto : list) {
            setLink(dto);
        }

        int count = countByQuery(query);
        query.setTotalCount(count);

        return list;
    }

    @Override
    public int readOneInnerMsg(Integer innerId) {
        if (innerId == null) {
            return 0;
        }
        return pushInfoDAO.readOneInnerMsg(innerId);
    }

    @Override
    public int updateOneInnerMsg(Integer innerId, Integer status) {
        if (innerId == null) {
            return 0;
        }

        return pushInfoDAO.updateOneInnerMsg(innerId, status);
    }

    @Override
    public int updateInnerMsgTemporaryStorageByStaffId(Integer staffId) {
        if (staffId == null) {
            return 0;
        }

        return pushInfoDAO.updateInnerMsgTemporaryStorageByStaffId(staffId);
    }

    @Override
    public InnerDTO findReadOneInnerMsg(Integer staffId) {
        if (staffId == null) {
            return null;
        }

        InnerDTO dto = findOneInnerMsg(staffId);
        if (dto != null) {
            readOneInnerMsg(dto.getId());

        }

        return dto;
    }

    private void setLink(InnerDTO dto) {
        String bData = dto.getLinkUrl();
        if (bData != null) {
            try {
                JSONObject json = (JSONObject)JSON.parse(bData);
                dto.setLinkUrl(json.get("linkUrl").toString());
                dto.setLinkName(json.get("linkName").toString());
            } catch (Exception e) {
                logger.error("[zmn]读取站内信时解析业务数据异常");
            }
        }
    }

    @Override
    public Integer insert(InnerDTO innerDTO) {
        Map<String, String> map = new HashMap<>();
        map.put("linkUrl", innerDTO.getLinkUrl());
        map.put("linkName", innerDTO.getLinkName());
        String businessData = JSON.toJSONString(map);
        PushInner pushInner = new PushInner();
        BeanUtils.copyProperties(innerDTO, pushInner);
        pushInner.setBusinessData(businessData);
        return pushInfoDAO.insert(pushInner);
    }

    @Override
    public Integer insertBatch(List<PushInner> pushInners) {
        return pushInfoDAO.insertBatch(pushInners);
    }
}
